import PubSub from 'pubsub-js';
import moment from 'moment';
import config from '../../../utils/config';
import request from '../../../utils/request';

// 获取全局实例
const appInstance = getApp();

Page({
    data: {
        isPlay: false, //是否播放
        music:{},
        musicId:'',
        url:'',
        currentTime:'00:00',//实时时间
        durationTime:'00:00',//总时长
        currentWidth: 0//实时进度条的宽度

    },
    onLoad: function(options){
        console.log('onLoad options:',options);
        const musicId = options.musicId;
        this.setData({
            musicId
        });
        this.getMusicInfo(musicId);

        // 判断进入该页面是否有音乐正在播放
        if(appInstance.globalData.isMusicPlay && appInstance.globalData.musicId == musicId){
            this.setData({
                isPlay: true
            });
        }

        
        //创建音乐播放暂停的实例
        this.backgroundAudioManager = wx.getBackgroundAudioManager();

        // 监视音乐播放/暂停
        // 修改音乐播放的状态
        this.backgroundAudioManager.onPlay(() => {
            console.log('onPlay');
            this.changeMusicStatus(true, musicId);
        });
        this.backgroundAudioManager.onPause(() => {
            console.log('onPause');
            this.changeMusicStatus(false, musicId);
        });
        
        // 监听音乐自然停止
        this.backgroundAudioManager.onStop(() => {
            console.log('onStop');
            this.changeMusicStatus(false, musicId);
        });
        
        // 监听音乐播放自然结束
        this.backgroundAudioManager.onEnded(() => {
            console.log('onEnded');
            PubSub.publish('switchType','next');
            this.setData({
                currentWidth: 0,
                currentTime: '00:00'
            });
        });

        // 监听音乐实时播放进度
        this.backgroundAudioManager.onTimeUpdate(() => {
            const currentTime = moment(this.backgroundAudioManager.currentTime * 1000).format('mm:ss')
            ;
            const currentWidth = this.backgroundAudioManager.currentTime / this.backgroundAudioManager.duration * 450;
            this.setData({
                currentTime,
                currentWidth
            });
        });
    },
    // 获取音乐详情
    async getMusicInfo(musicId){
        const res = await request(config.musicDetail,{ ids: musicId });
        const durationTime = moment(res.songs[0].dt).format('mm:ss');
        
        this.setData({
            music: res.songs[0],
            durationTime
        });

        // 动态修改当前窗口标题
        wx.setNavigationBarTitle({
            title: this.data.music.name
        });

    },
    // 点击播放/暂停
    handlePlay(){
        const isPlay = !this.data.isPlay;
        this.setData({
            isPlay
        });
        const { musicId,url } = this.data;
        this.musicController(isPlay, musicId,url);
    },
    // 控制音乐播放暂停
    async musicController(isPlay, musicId,url){
        if(isPlay){
            //获取音乐播放链接
            if(!url){
                const res = await request(config.musicUrl,{ id: musicId });
                url = res.data[0].url;
                this.setData({
                    url
                });
            }
            this.backgroundAudioManager.src = url;
            this.backgroundAudioManager.title = this.data.music.name;
        }else{
            // 暂停音乐
            this.backgroundAudioManager.pause();
        }
    },
    //音乐播放状态变更
    changeMusicStatus(isPlay,musicId){
        this.setData({
            isPlay
        });
        // 修改全局音乐播放状态
        appInstance.globalData.isMusicPlay = isPlay;
        appInstance.globalData.musicId = musicId;
    },
    // 点击切歌的回调
    handleSwitch(e){
        const type = e.currentTarget.id;

        // 关闭当前播放的音乐
        this.backgroundAudioManager.stop();

        // 发布消息数据给recommend-song页面
        PubSub.publish('switchType',type);

        // 订阅recommend-song页面的消息数据
        PubSub.subscribe('musicId',(msg,data) => {
            console.log('musicId:',data);
            this.getMusicInfo(data);
            this.musicController(true, data);

            //取消订阅，订阅会一直累加
            PubSub.unsubscribe('musicId');
        });
    }
});